package com.yuanduan.mplus.view;

import android.app.ProgressDialog;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.cj.framework.app.BaseActivity;
import com.cj.framework.util.HttpImp;
import com.cj.framework.util.IHttp;
import com.google.gson.Gson;
import com.yuanduan.mplus.R;
import com.yuanduan.mplus.config.Config;
import com.yuanduan.mplus.model.entity.RSM;
import com.yuanduan.mplus.model.entity.RSUserID;

import java.util.HashMap;

/**
 * 重置密码
 */
public class ResetPwdActivity extends BaseActivity implements View.OnClickListener {
    private EditText et_phone;
    private EditText et_code;
    private Button btn_getCode;
    private Button btn_next;
    private ImageButton ib_back;

    private String phone;
    private String code;
    private int UserID;
    private boolean isGetCode=false;


    private ProgressDialog progressDialog;
    private IHttp iHttp=new HttpImp();
    private IHttp.HttpInfoCallbackWithIndex callback=new IHttp.HttpInfoCallbackWithIndex() {
        @Override
        public void onSucceed(Object result, int index) {
            switch (index){
                case 0:
                    RSUserID rsUserID= (RSUserID) Config.decry(result,RSUserID.class);
                    if(rsUserID.getRS()==1){
                        UserID=rsUserID.getUserID();
                        isGetCode=true;
                        Toast.makeText(ResetPwdActivity.this,"发送验证码成功",Toast.LENGTH_SHORT).show();
                    }else if(rsUserID.getRS()==-1){
                        Toast.makeText(ResetPwdActivity.this,"生成验证码失败，请重试",Toast.LENGTH_SHORT).show();
                    }else if(rsUserID.getRS()==-2){
                        Toast.makeText(ResetPwdActivity.this,"无此用户,请检查手机号码",Toast.LENGTH_SHORT).show();
                    }
                    break;
                case 1:
                    progressDialog.dismiss();
                    RSM rsm=(RSM)Config.decry(result,RSM.class);
                    if(rsm.getRS()==1){
                        //成功；
                        Log.d("xx","1 success");
                        Bundle bundle=new Bundle();
                        bundle.putString("UserID",UserID+"");
                        openActivity(ResetPwd1Activity.class,bundle);
                    }else {
                        //失败
                        Toast.makeText(ResetPwdActivity.this,"验证码错误或超时",Toast.LENGTH_SHORT).show();
                    }
                    break;
            }
        }

        @Override
        public void onFailed(Object result, int index) {

        }
    };

    @Override
    protected void findId() {
        et_code= (EditText) findViewById(R.id.resetPwd_et_checkcode);
        et_phone= (EditText) findViewById(R.id.resetPwd_et_phone);
        btn_getCode= (Button) findViewById(R.id.resetPwd_btn_getCode);
        btn_next= (Button) findViewById(R.id.resetPwd_btn_next);
        ib_back= (ImageButton) findViewById(R.id.resetPwd_ib_back);
    }

    @Override
    protected void logic() {
        progressDialog=new ProgressDialog(this);
//        et_phone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
//            @Override
//            public void onFocusChange(View v, boolean hasFocus) {
//                if(!hasFocus){
//                    phone=et_phone.getText().toString().trim();
//                }
//            }
//        });
        btn_getCode.setOnClickListener(this);
        btn_next.setOnClickListener(this);
        ib_back.setOnClickListener(this);
    }

    @Override
    protected int setLayout() {
        return R.layout.activity_reset_pwd;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.resetPwd_btn_getCode:
                phone=et_phone.getText().toString().trim();
                //设置数字倒计时
                //发送验证码;
                if(TextUtils.isEmpty(phone)){
                    return;
                }
                countDown();
                HashMap<String,String> hashMap=new HashMap<>();
                hashMap.put("UserPhone",phone);
                iHttp.httpInfoPostWithIndex(Config.URL+Config.RESET_CODE,Config.translateMap(hashMap),callback,0);
                break;
            case R.id.resetPwd_btn_next:
                phone=et_phone.getText().toString().trim();
                code=et_code.getText().toString().trim();
                if(!isGetCode){
                    return;
                }
                //发送网络请求;
                //progressdialog;
                if(TextUtils.isEmpty(code)||TextUtils.isEmpty(phone)){
                    return;
                }
                progressDialog.show();
                HashMap<String,String> hashMap1=new HashMap<>();
                hashMap1.put("UserPhone",phone);
                hashMap1.put("Code",code);
                iHttp.httpInfoPostWithIndex(Config.URL+Config.INTO_RESETPWD,Config.translateMap(hashMap1),callback,1);
                break;
            case R.id.resetPwd_ib_back:
                this.finish();
                break;
        }
    }

    private Handler handler=new Handler();
    private int counts=120;
    private Runnable runnable=new Runnable() {
        @Override
        public void run() {
            if(counts>0){
                btn_getCode.setText(counts+" s");
                counts--;
                handler.postDelayed(this,1000);
            }else{
                btn_getCode.setText("获取验证码");
                counts=120;
                btn_getCode.setClickable(true);
            }
        }
    };

    private void countDown(){
        btn_getCode.setClickable(false);
        handler.postDelayed(runnable,100);
    }
}
